package com.atguigu.day03;

import java.lang.invoke.VarHandle;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;

//减少计数   CountDownLatch
public class CountDownLatchDemo {

    public static void main(String[] args) throws InterruptedException {
        //共八个线程，现在需要这八个线程都执行完毕才能让main方法内的线程执行
        CountDownLatch countDownLatch = new CountDownLatch(5);
        //需要使用CountDownLatch，线程执行一个，该数就从初始值减少一，直到减为0，才开始执行main方法中的线程
//         new Thread(() -> {
             for (int i = 1; i <=5 ; i++) {
                 int finalI = i;
//                 try {TimeUnit.SECONDS.sleep(1);} catch (InterruptedException e) {throw new RuntimeException(e);}
                 new Thread(() -> {
                     System.out.println(finalI +"正在执行。。。");
                     //一个线程执行完毕，就减一
                     countDownLatch.countDown();
                 }, String.valueOf(i)).start();
             }
//                 }, "A").start();

        countDownLatch.await();
        //线程睡眠几秒
//        try {
//            TimeUnit.SECONDS.sleep(1);} catch (InterruptedException e) {throw new RuntimeException(e);}
        System.out.println("main~关门走人。。。");
    }
}
